import {
	reactive,
	ref
} from "vue";

import {uploadFile} from "@/network/home.js"
/**
 *  图片上传
 * count图片数量
 * sourceType类型
 */
export const receipt = (count = 1, sourceType = ['original', 'compressed'], crop = "{quality:60,}", sizeType =
	"['compressed']") => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count,
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['camera'], //从相册选择
			crop: crop,
			success: chooseImageRes => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				let imagedatapromis = tempFilePaths.map((itrm, index) => {
					return new Promise((resolve, reject) => [
						uploadFile(itrm).then(res => {
							resolve(res.data)
						}).catch(Error => {
							console.log(Error);
							uni.showToast({
								icon: "error",
								title: "图片上传失败"
							})
							reject(Error)
						})
					])



				});
				uni.showLoading({
					title: "图片上传中"
				})
				Promise.all(imagedatapromis).then(res => {
					uni.hideLoading()
					resolve(res)
				}).catch(err => {
					uni.hideLoading()
					uni.showToast({
						icon: "error",
						title: "图片上传失败"
					})
					reject(err)
				})
			},

		})
	}).catch(err => {
		uni.hideLoading()
		reject(err)
	})
}